#ifdef __cplusplus
extern "C" {
#endif

#include "dlist.h"
#include <malloc.h>
typedef struct _DListNode
{
	struct _DListNode* prev;
	struct _DListNode* next;
	void* data;
}DListNode;

typedef struct _DList
{
	DListNode* 	first;
	DListNode* 	last;
	unsigned long 	storage;
}DList;

DListRet dlist_print(DList* thiz, DListDataPrintFunc print)
{
	DListNode* iter = thiz->first;
	while (iter){
		print(iter->data);
		iter = iter->next;
	}

	return DLIST_OK;
}

DListRet dlist_foreach(DList* thiz, DListVisitFunc visit, void * ctx)
{
	DListRet ret	= DLIST_OK;
	DListNode* iter = thiz->first;
	while(iter && ret != DLIST_STOP){
		ret = visit(ctx, iter->data);
		iter = iter->next;
	}
	return ret;
}

DList* dlist_create(void* data)
{
	DList* plist = malloc(sizeof(DList));
	DListNode* pnode = NULL;

	plist = malloc(sizeof(DList));
	if (!plist){
		return NULL;
	}
	pnode = malloc(sizeof(DListNode));
	if(!pnode){
		free(plist);
		return NULL;
	}

	plist->first = plist->last = pnode;
	plist->storage = 1;
	pnode->prev = pnode->next = NULL;
	pnode->data = data;
	return plist;
}

DListRet dlist_destroy(DList* Thiz)
{
	DListNode* pnode = Thiz->first;

	while(pnode){
		Thiz->first = pnode->next;
		free(pnode);
		pnode = Thiz->first;
	}
	free(Thiz);
	return DLIST_OK;
}

DListRet dlist_insert(DList* Thiz)
{
	return DLIST_OK;
}
#ifdef __cplusplus
}
#endif
